#include<iostream>
#include<algorithm>
using namespace std;

class Solution {
public:
    vector<int> sortedSquaresBasic(vector<int>& nums) {
        for(int i=0;i<nums.size();i++)
        {
            nums[i]=nums[i]*nums[i];
        }
        sort(nums.begin(),nums.end());
        return nums;
    }

    vector<int> sortedSquares(vector<int>& nums) {
        int l=0,r=nums.size()-1;
        vector<int> v(nums.size());
        for (int i =nums.size()-1 ; i >= 0; i--)
        {
            if(abs(nums[r])>abs(nums[l]))
            {
                v[i]=nums[r]*nums[r];
                r--;
            }
            else
            {
                v[i]=nums[l]*nums[l];
                l++;
            }
        }
        return v;
        
    }

    void printvector(vector<int>& nums)
    {
        for(int i=0;i<nums.size();i++)
        {
            cout<<nums[i]<<" ";
        }
    }
};

int main()
{
    int n;
    cout<<"size:";
    cin>>n;
    vector<int> v1(n);
    for(int i=0;i<n;i++)
    {
        cout<<"input number "<<i+1<<":";
        cin>>v1[i];
    }

    Solution s;
    vector<int> v2= s.sortedSquares(v1);
    s.printvector(v2);
}
